package in.hitmedia.test.misc;

import java.util.ArrayList;

public class Permutations 
{
	
	private ArrayList<String> permutate(String str)
	{
		if (str == null || str.length() == 0)
		{
			ArrayList<String> p = new ArrayList<String>();
			p.add("");
			return p;
		}
	
		char c = str.charAt(0);
		ArrayList<String> p = new ArrayList<String>();
		ArrayList<String> words = permutate(str.substring(1));
		for(String s:words)
		{
			int len = s.length();
			for(int i=0; i <= len; ++i)
			{
				p.add(insertCharInWord(s, c, i));
			}
		}
		return p;
		
	}
	
	private String insertCharInWord(String s, char c, int i)
	{
		String a = s.substring(0,i);
		String b = s.substring(i);
		return a + c + b;
	}

	public static void main(String args[])
	{
		Permutations perms = new Permutations();
		ArrayList<String> ret = perms.permutate("abcde");
		
		System.out.println("Length: "+ret.size());
		for(String s : ret)
		{
			System.out.println(s);
		}
	}
	
}
